package com.bookworld.model;

import jakarta.validation.constraints.*;
import org.springframework.format.annotation.NumberFormat;

import java.math.BigDecimal;

public class OrderItem {
    private Long id;
    
    @NotNull(message = "{orderItem.order.notnull}")
    private Long orderId;
    
    @NotNull(message = "{orderItem.book.notnull}")
    private Long bookId;
    
    @NotNull(message = "{orderItem.quantity.notnull}")
    @Min(value = 1, message = "{orderItem.quantity.min}")
    @Max(value = 999, message = "{orderItem.quantity.max}")
    private Integer quantity;
    
    @NotNull(message = "{orderItem.price.notnull}")
    @DecimalMin(value = "0.01", message = "{orderItem.price.min}")
    @NumberFormat(pattern = "#,##0.00")
    private BigDecimal price;
    
    @DecimalMin(value = "0.00", message = "{orderItem.discount.min}")
    private BigDecimal discount;
    
    @NotNull(message = "{orderItem.finalPrice.notnull}")
    @DecimalMin(value = "0.01", message = "{orderItem.finalPrice.min}")
    private BigDecimal finalPrice;
    
    private String notes;
    
    // 关联对象
    private Book book;
    private Order order;

    public OrderItem() {
        this.discount = BigDecimal.ZERO;
    }

    // 计算最终价格
    public void calculateFinalPrice() {
        if (price != null && discount != null) {
            this.finalPrice = price.subtract(discount);
        }
    }

    // 计算小计
    public BigDecimal getSubtotal() {
        if (finalPrice != null && quantity != null) {
            return finalPrice.multiply(BigDecimal.valueOf(quantity));
        }
        return BigDecimal.ZERO;
    }

    // Getters and Setters
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getOrderId() {
        return orderId;
    }

    public void setOrderId(Long orderId) {
        this.orderId = orderId;
    }

    public Long getBookId() {
        return bookId;
    }

    public void setBookId(Long bookId) {
        this.bookId = bookId;
    }

    public Integer getQuantity() {
        return quantity;
    }

    public void setQuantity(Integer quantity) {
        this.quantity = quantity;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    public BigDecimal getDiscount() {
        return discount;
    }

    public void setDiscount(BigDecimal discount) {
        this.discount = discount;
    }

    public BigDecimal getFinalPrice() {
        return finalPrice;
    }

    public void setFinalPrice(BigDecimal finalPrice) {
        this.finalPrice = finalPrice;
    }

    public String getNotes() {
        return notes;
    }

    public void setNotes(String notes) {
        this.notes = notes;
    }

    public Book getBook() {
        return book;
    }

    public void setBook(Book book) {
        this.book = book;
    }

    public Order getOrder() {
        return order;
    }

    public void setOrder(Order order) {
        this.order = order;
    }

    @Override
    public String toString() {
        return "OrderItem{" +
                "id=" + id +
                ", bookId=" + bookId +
                ", quantity=" + quantity +
                ", price=" + price +
                ", finalPrice=" + finalPrice +
                '}';
    }
}
